import { ColumnType } from 'antd/lib/table';
import { DocumentCasSchema, DocumentSchema } from "@src/components/schema/document"
import { Space, message, Tooltip } from 'antd';
import style from './index.module.less'
import { Dispatch } from 'redux';
import { addWindow } from "@src/redux/actions/tab"
import { awaitWrap, confirmAction, formatDate } from '@src/assets/js/tool'
import { delRequest } from './ajax'
import truePng from '@imgs/true.png'
import falsePng from '@imgs/false.png'

async function delHandle (id: string, refreshFn: () => void) {
  const h = message.loading('作废中', 120)
  const [e] = await awaitWrap(delRequest(id))
  h()
  if (e === null) {
    refreshFn()
  }
}

 // 获取表格字段配置
 export function getColumns (caseid: string, dispatch: Dispatch, refreshFn: () => void): Array<ColumnType<DocumentSchema>> {
  const list: Array<ColumnType<DocumentSchema>> = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: 60
    },
    {
      title: '发起人',
      dataIndex: 'creator',
      key: 'creator'
    },
    {
      title: '文件名称',
      dataIndex: 'title',
      key: 'title',
      render: (v, line) => {
        return v || line.filename
      }
    },
    {
      title: '发送对象',
      dataIndex: 'documentcas',
      key: 'documentcas',
      render: (v: DocumentCasSchema[]) => {
        return (
          <span>
            {
              v.map((li, i) => {
                const t = ['签署成功', '完成'].includes(li.status)
                const f = ['签署失败'].includes(li.status)
                return (
                  <span className={style.cas} key={i}>
                    <span className={style['cas-name']}>
                      <span>{li.name}</span>
                      {(t || f) && (
                        <Tooltip title={li.status}>
                          <img className={style['cas-status']} src={t ? truePng : falsePng} />
                        </Tooltip>
                      )}
                    </span>
                  </span>
                )
              })
            }
          </span>
        )
      }
    },
    {
      title: '发送时间',
      dataIndex: 'seal_send_datetime',
      key: 'seal_send_datetime',
      render: (v: number) => {
        if (typeof v === 'number') {
          return formatDate(v, 'YYYY-MM-DD HH:mm')
        }
      }
    },
    {
      title: '更新时间',
      dataIndex: 'seal_datetime',
      key: 'seal_datetime',
      render: (v: number) => {
        if (typeof v === 'number') {
          return formatDate(v, 'YYYY-MM-DD HH:mm')
        }
      }
    },
    {
      title: '状态',
      dataIndex: 'seal_status',
      key: 'seal_status'
    },
    {
      title: '操作',
      dataIndex: 'index',
      key: 'operate',
      render: (value: number, line: DocumentSchema) => {
        const editable = line.seal_status === '未发起'
        return (
          <Space>
            <a onClick={() => dispatch(addWindow({ id: `document-sign-${line.id}`, title: `${line.title || line.filename}`, componentName: 'PdfSignReader', forceRefresh: false, meta: { id: line.id, editable, caseid } }))}>
              {editable ? '编辑' : '详情'}
            </a>
            <a className={style.del} onClick={() => confirmAction('您确定要作废该文书吗？', () => { delHandle(line.id, () => refreshFn()) })}>作废</a>
          </Space>
        )
      }
    }
  ]
  return list
}